package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.EducationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/9/7.
 * 学历
 */

public class EducationActivity extends Activity implements View.OnClickListener{
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.education_text)//学历
            TextView education_text;
    @Bind(R.id.degree_type_text)//学历类型
            TextView degree_type_text;
    @Bind(R.id.admission_date_text)//入学时间
            TextView admission_date_text;
    @Bind(R.id.graduation_text)//毕业结论
            TextView graduation_text;
    @Bind(R.id.graduation_date_text)//毕业日期
            TextView graduation_date_text;
    @Bind(R.id.majors_text)//专业
            TextView majors_text;
    @Bind(R.id.school_text)//学校
            TextView school_text;
    @Bind(R.id.educational)//学历展示
    LinearLayout reteducationalur;

    @Bind(R.id.available_layout)//学历无信息
            LinearLayout available_layout;
    Context context;
    private MyProgressDialog mDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_education);
        ButterKnife.bind(this);
        context=this;
        initView();
    }
    private void initView(){
        retur.setOnClickListener(this);
        initData();
    }
    private void initData(){
            if (Sundry.isOpenNetwork(context)){
                mDialog =new MyProgressDialog(context);
                HttpClient.academicDetails(new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        if (mDialog!=null){
                            mDialog.dismiss();
                        }
                        System.out.println("stringmd5result"+result);
                        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
                        if (getnote!=null||getnote.size()>0){
                            String status = getnote.get(0).getStatus();
                            String getMsg = getnote.get(0).getMsg();
                            if (status.equals("200")){
                                String content = getnote.get(0).getContent();
                                List<EducationModel> education = JsonTools.getEducation(content);
                                if (education!=null||education.size()>0){
                                    reteducationalur.setVisibility(View.VISIBLE);
                                    available_layout.setVisibility(View.GONE);
                                    processData(education);
                                }
                                ToastUtils.show(context, getMsg);
                            }else if (status.equals("105")){
                                Intent intent = new Intent(context, LoginActivity.class);
                                startActivity(intent);
                                ToastUtils.show(context, getMsg);
                            }else {
                                reteducationalur.setVisibility(View.GONE);
                                available_layout.setVisibility(View.VISIBLE);
                                ToastUtils.show(context, getMsg);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        if (mDialog!=null){
                            mDialog.dismiss();
                        }
                        reteducationalur.setVisibility(View.GONE);
                        available_layout.setVisibility(View.VISIBLE);
                        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {
                        if (mDialog!=null){
                            mDialog.dismiss();
                        }
                        reteducationalur.setVisibility(View.GONE);
                        available_layout.setVisibility(View.VISIBLE);
                        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
                    }

                    @Override
                    public void onFinished() {

                    }
                });
            }else {
                ToastUtils.Toastshow(context, getResources().getString(R.string.network));
            }
    }
    //绑定数据
    private void processData( List<EducationModel> education ){
        EducationModel educationModel = education.get(0);
        education_text.setText(educationModel.getEducation());
        degree_type_text.setText(educationModel.getDegreetype());
        admission_date_text.setText(educationModel.getEnrolldate());
        graduation_text.setText(educationModel.getGradresult());
        graduation_date_text.setText(educationModel.getGraddate());
        majors_text.setText(educationModel.getMajor());
        school_text.setText(educationModel.getSchool());
    }
    @Override
    public void onClick(View view) {
        finish();
    }
}
